package com.syq.dao.impl;

import com.syq.bean.Role;
import com.syq.bean.Student;
import com.syq.bean.Users;
import com.syq.dao.UsersDao;
import com.syq.util.DBUtils;
import com.syq.util.StudentEnum;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UsersDaoImpl extends DBUtils implements UsersDao {
    @Override
    public Users login(String userName, String userPassword) {
        Users user = null;
        try {
            String sql = "select * from users where loginname=? and password=?";
            List list = new ArrayList();
            list.add(userName);
            list.add(userPassword);
            resultSet = query(sql, list);

            while (resultSet.next()) {
                user = new Users();
                user.setUserId(resultSet.getInt("userid"));
                user.setLoginName(resultSet.getString("loginname"));
                user.setPassWord(resultSet.getString("password"));
                user.setRealName(resultSet.getString("realname"));
                user.setRoleId(resultSet.getInt("roleid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return user;
    }

    @Override
    public List<Users> getUsersList(int pageIndex, int pageSize) {
        List<Users> usersList=new ArrayList<Users>();
        try {
            String  sql = "select userid,loginname,realname,rolename from users u,role r where u.roleid=r.roleid limit ?,?";
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            resultSet = query(sql,params);
            while (resultSet.next()) {
                //取出Users
                Users users = new Users();
                users.setUserId(resultSet.getInt("userid"));
                users.setLoginName(resultSet.getString("loginname"));
                users.setRealName(resultSet.getString("realname"));

                Role role = new Role();
                role.setRoleName(resultSet.getString("rolename"));
                //2.建立关系
                users.setRole(role);
                usersList.add(users);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return usersList;
    }

    @Override
    public int total() {
        int toatal = 0;
        try {
            String  sql = "select count(1) from users u,role r where u.roleid=r.roleid ";

            resultSet = query(sql,null);
            while (resultSet.next()) {
                toatal = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return toatal;
    }

    @Override
    public int insertUser(Users user) {
        int i = 0;
        try {
            String sql = "insert into users values(null,?,?,?,?,?,?,?,?,?,?)";
            List params = new ArrayList();
            params.add(user.getLoginName());
            params.add(user.getPassWord());
            params.add(user.getRealName());
            params.add(user.getSex());
            params.add(user.getEmail());
            params.add(user.getAddress());
            params.add(user.getPhone());
            params.add(user.getCardId());
            params.add(user.getDesc());
            params.add(user.getRoleId());
            i = update(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return i;
    }

    @Override
    public int updateUser(Users user) {
        String sql = "update users set desct=?,roleid=?,loginname=?,password=?,realname=?,sex=?,email=?,address=?,phone=?,cardid=? where userid=?";
        List params = new ArrayList();
        params.add(user.getDesc());
        params.add(user.getRoleId());
        params.add(user.getLoginName());
        params.add(user.getPassWord());
        params.add(user.getRealName());
        params.add(user.getSex());
        params.add(user.getEmail());
        params.add(user.getAddress());
        params.add(user.getPhone());
        params.add(user.getCardId());

        params.add(user.getUserId());
        int count = update(sql,params);
        return count;
    }

    @Override
    public Users findById(int userId) {
        String sql = "select * from users where userid=?";
        List params = new ArrayList();
        Users user = new Users();
        try {
            params.add(userId);
            resultSet = query(sql,params);
            while (resultSet.next()) {
               user.setUserId(userId);
               user.setLoginName(resultSet.getString("loginname"));
               user.setRealName(resultSet.getString("realname"));
               user.setPassWord(resultSet.getString("password"));
               user.setSex(resultSet.getInt("sex"));
               user.setEmail(resultSet.getString("email"));
               user.setAddress(resultSet.getString("address"));
               user.setPhone(resultSet.getString("phone"));
               user.setCardId(resultSet.getString("cardid"));
               user.setDesc(resultSet.getString("desct"));
               user.setRoleId(resultSet.getInt("roleid"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return user;
    }

    @Override
    public int delUser(String uid) {
        int count =0;
        try {
            String sql = "delete from users where userid=?";
            List params = new ArrayList();
            params.add(uid);
            count = update(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }

        return count;
    }

}
